package button;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.sam.widget.R;


public class CountDownButton extends Button {
    private Activity activity;

    private int startCountNum = 60,endCountNum = 0;
    private String defaultText,tickText,finishText;

    public CountDownButton(Context context) {
        this(context,null);
    }

    public CountDownButton(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CountDownButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        if(context instanceof Activity){
            this.activity = (Activity) context;
        } else throw new IllegalAccessError("倒计时按钮只能在Activity或其子类中使用");

        getAttrs(attrs);
    }

    private void getAttrs(AttributeSet attrs){
        TypedArray ta = activity.obtainStyledAttributes(attrs, R.styleable.CountDownButton);
        startCountNum = ta.getInt(R.styleable.CountDownButton_startCountNum,startCountNum);
        endCountNum = ta.getInt(R.styleable.CountDownButton_endCountNum,endCountNum);
        defaultText = ta.getString(R.styleable.CountDownButton_defaultText);
        tickText = ta.getString(R.styleable.CountDownButton_tickText);
        finishText = ta.getString(R.styleable.CountDownButton_finishText);
        ta.recycle();
        init();
    }

    public void init(){
        final boolean descrease = startCountNum > endCountNum;
        final long totalMillis = Math.abs(startCountNum - endCountNum) * 1000L;
        setText(defaultText);
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("sam","clicked……………………………………");
                new CountDownBtnThread(totalMillis,1000,descrease).start();
                if(onButtonClickListener != null) onButtonClickListener.onClick();
            }
        });
    }


    class CountDownBtnThread extends CountDownTimer {
        private int time;
        private boolean decrease;

        public CountDownBtnThread(long millisInFuture, long countDownInterval,boolean decrease){
            this(millisInFuture,countDownInterval);
            this.decrease = decrease;
        }

        public CountDownBtnThread(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
            time = (int)(millisInFuture / countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if(tickText.contains("%1$d")){
                        setText(String.format(tickText,(time = decrease ? time-1 : time+ 1)));
                    } else setText(tickText);
                }
            });

        }

        @Override
        public void onFinish() {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    setText(finishText);
                    setEnabled(true);
                }
            });
        }
    }


    private OnButtonClickListener onButtonClickListener;

    public interface OnButtonClickListener {
        void onClick();
    }

    public void setOnButtonClickListener(OnButtonClickListener onButtonClickListener) {
        this.onButtonClickListener = onButtonClickListener;
    }
}
